package com.resou.reader.reader.autopay;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.R;
import com.resou.reader.api.entry.BookDetailBean;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.data.bookshelf.entity.Book;
import com.resou.reader.reader.adpter.AutoPayAdapter;
import com.resou.reader.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 自动订购
 */
public class AutoPayActivity extends ResouBaseActivity<AutoPayPresenter> implements ContractAutoPay.AutoPayView {


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.tvAutoPay)
    TextView tvAutoPay;
    int page = 0;
    AutoPayAdapter autoPayAdapter;

    List<BookDetailBean> list = null;
    int count = 0;
    StringBuffer buffer;

    /**
     * 启动自动订购的方法
     *
     * @param context
     */
    public static void start(Context context) {
        Intent starter = new Intent(context, AutoPayActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        if (mPresenter == null) {
            mPresenter = new AutoPayPresenter(this);
        }
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        tvTitle.setText(R.string.autopay);
        if (mPresenter == null) {
            initPresenter();
        }
        autoPayAdapter = new AutoPayAdapter(R.layout.autopayadapter, null);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(autoPayAdapter);
        mSmartRefreshLayout.setEnableRefresh(false);
        mSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                mPresenter.quearAutoPayList(page);
            }
        });
        autoPayAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                list = adapter.getData();
                list.get(position).setIsCheck(!list.get(position).getIsCheck());
                autoPayAdapter.setNewData(list);
                setDataCheck();
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_auto_pay;
    }

    @Override
    public void retryRefreshData() {
        page = 0;
        mPresenter.quearAutoPayList(page);
    }

    @Override
    public void initData() {
        mPresenter.quearAutoPayList(page);
    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        showLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        showContent();
        mSmartRefreshLayout.finishLoadMore();
        mSmartRefreshLayout.finishRefresh();
        if (page == 0) {
            showNetErrorView(R.id.rlContent);
        }
    }

    @Override
    public void showEmpty() {
        autoPayAdapter.setEmptyView(getEmptyView());
        if (list != null) {
            list.clear();
        }
        tvAutoPay.setVisibility(View.GONE);
        setDataCheck();
    }

    @OnClick({R.id.ivBack, R.id.tvAutoPay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvAutoPay:
                Log.i("选中的书籍：", "onClick: " + buffer.toString());
                mPresenter.cancellAutoPay(buffer.toString());
                break;
        }
    }

    @Override
    public void showAutoPayList(List<BookDetailBean> lists) {
        if (page == 0) {
            if (list != null) {
                list.clear();
            }
            autoPayAdapter.setNewData(lists);
        } else {
            autoPayAdapter.addData(lists);
        }
        if (autoPayAdapter.getData().size() > 0) {
            tvAutoPay.setVisibility(View.VISIBLE);
        } else {
            tvAutoPay.setVisibility(View.GONE);
        }
        setDataCheck();
    }

    @Override
    public void cancellAutoSuccess() {
        ToastUtil.makeShortToast("取消订阅成功");
        page = 0;
        mPresenter.quearAutoPayList(page);
    }

    @Override
    public SmartRefreshLayout getSmartRefreshLayout() {
        return mSmartRefreshLayout;
    }


    private void setDataCheck() {
        count = 0;
        if (list != null) {
            buffer = new StringBuffer("");
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getIsCheck()) {
                    count++;
                    buffer.append(list.get(i).getId() + ",");
                }
            }
        }
        if (count > 0) {
            tvAutoPay.setText("取消自动购买(" + count + ")");
            tvAutoPay.setBackgroundColor(getResources().getColor(R.color.theme_color));
            tvAutoPay.setClickable(true);
        } else {
            tvAutoPay.setText("取消自动购买");
            tvAutoPay.setBackgroundColor(getResources().getColor(R.color.color_999));
            tvAutoPay.setClickable(false);
        }
    }

    /**
     * 空页面数据
     */
    private View getEmptyView() {
        View view = LayoutInflater.from(this).inflate(R.layout.empty_layout, null);
        ImageView iv = view.findViewById(R.id.ivnoBooklistIcon);
        iv.setImageResource(R.mipmap.ic_autopay_empty);
        TextView tvInfo = view.findViewById(R.id.tvShowInfo);
        tvInfo.setText("您还没有授权自动购买的书籍");
        return view;
    }
}
